﻿package  {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class MainPanel extends MovieClip {
		
		private const stageWidth = 680;
		private const stageHight = 560;
		private const baseCellNum = 15;
		private const cellKindNum = 6;
		private var round:uint = 0;//关数
		private var maxCellNum:uint;//每关单元格数=baseCellNum + (round - 1) * 2;
		public static const cellWidth = 50;
		private static const distroyFrame = 24;//关键帧
		
		//游戏背景
		private var bgMc:MovieClip;
		private const bgMcTopY = 70;
		private const bgMcLeftX = 0;
		
		//菜单栏
		private var toolbarMc:MovieClip;
		
		//路径用
		private var pathArr:Array;//路线坐标，放置的是路径按顺序的[currentX，currentY]
		private var pathPointArr:Array;//放置的是整个矩阵的PathPoint对象
		private var pathSp:Sprite;
		
		private var monkeyMc:MovieClip;
		private var aimMc:Sprite;
		
		//元素单元格
		private var cellArr:Array;//放置的是整个矩阵的元素
		//private const visualRowNum = 14;//可视矩阵最大列数 680/50
		//private const visualLineNum = 12;//可视矩阵最大行数 560/50
		private var elementSp:Sprite;
		
		//配对用
		public static var mouseEnableFlag:Boolean;//false 不可用 true 可用
		public static var monkeyWalkFlag:Boolean;//false 未行走 true 行走中
		public static var selectedFlag:Boolean;//false 未选中 true 已选中
		public static var mixArr:Array;//用于存放待检测的中心元素
		private var removeElemArr:Array;//用于放置以本元素为中心的有待消除的元素
		private var dropArr:Array;//按x位，明确各列元素降落单位数
		
		private var frameCount:uint;
		
		public function MainPanel() {
			monkeyMc = new Monkey();
			aimMc = new Aim();
			
			bgMc = new MovieClip();
			this.addChild(bgMc);
			
			toolbarMc = new Toolbar();
			this.addChild(toolbarMc);
			
			init();
			this.addEventListener(GameEvent.BgMcMove, setBgMcPosition);
			this.addEventListener(GameEvent.CheckRemove, checkRemoveItemHandler);
			this.addEventListener(GameEvent.DoRemove, doRemoveHandler);
			this.addEventListener(GameEvent.ElementsDrop, elementsDropHandler);
		}
		
		//每关初始化
		private function init():void{
			//清除所有背景元素
			while(bgMc.numChildren > 0)
				//bgMc预留4层，自上而下分别是
				//1.主角和目的地层
				//2.element层
				//3.路径层
				//4.背景层
				bgMc.removeChildAt(bgMc.numChildren - 1);
			
			round ++;
			maxCellNum = baseCellNum + (round - 1) * 2;
			monkeyWalkFlag = false;
			
			pathArr = new Array();
			cellArr = new Array(maxCellNum);
			pathPointArr = new Array(maxCellNum);
			for(var j:uint; j < maxCellNum; j ++){
				cellArr[j] = new Array(maxCellNum);
				pathPointArr[j] = new Array(maxCellNum);
			}
			
			//新建一图层，放置pathPoint
			pathSp = new Sprite();
			
			//动态定路线
			setPath(maxCellNum, maxCellNum);
			
			//完成之后，将图层加入bgMc
			bgMc.addChild(pathSp);
			
			//设置主角和目的地位置
			bgMc.addChild(monkeyMc);
			setRealPosition(monkeyMc, pathArr[0][0], pathArr[0][1]);
			var posArr:Array = pathArr[0];
			monkeyMc.setPosition(posArr);
			pathArr.shift();
			
			//monkeyMc.dispatchEvent(new GameEvent(GameEvent.MonkeyMove, [pathArr]));
			
			bgMc.addChild(aimMc);
			var tmpLen:uint = pathArr.length - 1;
			setRealPosition(aimMc, maxCellNum, pathArr[tmpLen][1]);
			aimMc.x = maxCellNum * cellWidth;
			
			//设置以主角为中心的bgMc的坐标
			//this.dispatchEvent(new GameEvent(GameEvent.BgMcMove));
			setBgMcPosition(null);
			
			//元素就位
			elementSp = new Sprite();
			
			bgMc.addChild(elementSp);
			bgMc.swapChildren(elementSp, monkeyMc);
			
			//计算元素矩阵
			calculateMatrix();
			
			//可视矩阵的显示动画
			visualMatrixAinimation();
			
			selectedFlag = false;
			mouseEnableFlag = true;
			mixArr = new Array();			
			removeElemArr = new Array();
			
			dropArr = new Array(maxCellNum);
			for(var d:uint = 0; d < maxCellNum; d ++){
				dropArr[d] = 0;
			}
		}
		
		//动态定路线
		private function setPath(indexX:uint, indexY:uint):void{
			if(indexX == maxCellNum){
				//起始位置，x固定为0，y在最大值以内随机
				indexX = 0;
				indexY = uint(Math.random() * maxCellNum);
			}
			else{
				//只要x为maxCellNum，跳出递归
				if(indexX == maxCellNum - 1) return;
				
				//依据pathArr[pathArr.length - 1]值，最多有3个可以走的路径
				//依据pathPointArr，将其中不可走的排除
				var tmpPointArr:Array = new Array();
				//if(indexX > 0 && isHaveElement(pathPointArr, indexX - 1, indexY) == false)
					//tmpPointArr.push([indexX - 1, indexY]);
				if(indexY > 0 && isHaveElement(pathPointArr, indexX, indexY - 1) == false)
					tmpPointArr.push([indexX, indexY - 1]);
				if(isHaveElement(pathPointArr, indexX + 1, indexY) == false) 
					tmpPointArr.push([indexX + 1, indexY]);
				if(indexY < maxCellNum - 1 && isHaveElement(pathPointArr, indexX, indexY + 1) == false) 
					tmpPointArr.push([indexX, indexY + 1]);
				
				//随机获取一个路径
				var rdmIndex:uint = uint(Math.random() * tmpPointArr.length);
				indexX = tmpPointArr[rdmIndex][0];
				indexY = tmpPointArr[rdmIndex][1];
			}
			
			//给pathArr push值
			var tmpPoint:Array = [indexX, indexY];
			pathArr.push(tmpPoint);
			
			//给图层放置pathPoint
			var pp = new PathPoint();
			pp.visible = false;
			
			//给pathPointArr set[x][y]位值
			pathPointArr[indexX][indexY] = pp;
			
			pathSp.addChild(pp);
			setRealPosition(pp, indexX, indexY);
			
			setPath(indexX, indexY);
		}
		
		//判断该位置是否有元素
		private function isHaveElement(aimArr:Array, aimX:uint, aimY:uint):Boolean{
			if(aimArr[aimX][aimY] == undefined) return false;
			return true;
		}
		
		//设置以主角为中心的bgMc的坐标
		private function setBgMcPosition(e:GameEvent):void{
			if(monkeyMc.y + stageHight * 0.5 > maxCellNum * cellWidth)
				bgMc.y = stageHight - maxCellNum * cellWidth;
			else if(monkeyMc.y - stageHight * 0.5 < bgMcTopY)
				bgMc.y = bgMcTopY;
			else
				bgMc.y = stageHight * 0.5 - monkeyMc.y;
			
			if(monkeyMc.x + stageWidth * 0.5 > maxCellNum * cellWidth)
				bgMc.x = stageWidth - maxCellNum * cellWidth;
			else if(monkeyMc.x - stageWidth * 0.5 < bgMcLeftX)
				bgMc.x = bgMcLeftX;
			else
				bgMc.x = stageWidth * 0.5 - monkeyMc.x;
			//设置该页面的起始currentX，currentY
			
		}
		
		//设置实际坐标
		private function setRealPosition(obj:DisplayObject, xIndex:uint, yIndex:int):void{
			if(xIndex < maxCellNum)obj.x = (xIndex + 0.5) * cellWidth;
			if(yIndex < maxCellNum)obj.y = (yIndex + 0.5) * cellWidth;
		}
		
		//计算元素矩阵
		private function calculateMatrix():void{
			cellArr = new Array(maxCellNum);
			for(var l:uint = 0; l < maxCellNum; l ++){
				cellArr[l] = new Array(maxCellNum);
				for(var m:uint = 0; m < maxCellNum; m ++){
					if(l == monkeyMc.getCurrentX() && m == monkeyMc.getCurrentY()){
						cellArr[l][m] = 0;
					}
					else{
						//计算当前元素的index
						var index:uint = getIndex(l, m);
						
						cellArr[l][m] = new Element(l, m, index);
					}
				}
			}
		}
		
		//计算当前元素的index
		private function getIndex(tmpL:uint, tmpM:uint, isNew:Boolean=true):uint{
			var tmpI = -1;
			var fourArr = new Array();
			for(var kindI:uint = 0; kindI < cellKindNum; kindI ++){
				fourArr[kindI] = kindI + 1;
			}
			
			//保证本元素不与左、上、右、下相同即可
			if(tmpM > 0){
				var eI = cellArr[tmpL][tmpM - 1];
				if(eI != undefined && eI != 0)
					tmpI = fourArr.indexOf((eI as Element).getIndex());
			}
			if(tmpI > -1)fourArr.splice(tmpI, 1);
			
			tmpI = -1;
			if(tmpL > 0){
				var eJ = cellArr[tmpL - 1][tmpM];
				if(eJ != undefined && eJ != 0)
					tmpI = fourArr.indexOf((eJ as Element).getIndex());
			}
			if(tmpI > -1)fourArr.splice(tmpI, 1);
			
			if(isNew == false){
				tmpI = -1;
				if(tmpM < maxCellNum - 1){
					var eK = cellArr[tmpL][tmpM + 1];
					if(eK != undefined && eK != 0)
						tmpI = fourArr.indexOf((eK as Element).getIndex());
				}
				if(tmpI > -1)fourArr.splice(tmpI, 1);
				
				tmpI = -1;
				if(tmpL < maxCellNum - 1){
					var eH = cellArr[tmpL + 1][tmpM];
					if(eH != undefined && eH != 0)
						tmpI = fourArr.indexOf((eH as Element).getIndex());
				}
				if(tmpI > -1)fourArr.splice(tmpI, 1);
			}
			
			var tLen:uint = fourArr.length;
			if(tLen == 0) return uint(Math.random() * cellKindNum + 1);
			return fourArr[int(Math.random() * fourArr.length)];
		}
		
		//可视矩阵的显示动画
		private function visualMatrixAinimation(){
			for(var vi:uint = 0; vi < maxCellNum; vi ++){//visualRowNum
				for(var vj:uint = 0; vj < maxCellNum; vj ++){//visualLineNum
					if(cellArr[vi][vj] == 0) continue;
					
					var elem = cellArr[vi][vj];
					elementSp.addChild(elem);
					elem.x = (vi + 0.5) * cellWidth;
					elem.y = (vj + 0.5) * cellWidth - maxCellNum * cellWidth;
					setRealPosition(elem, vi, vj);
					
					//elem.dispatchEvent(new GameEvent(GameEvent.ElementDrop, [maxCellNum]));
				}
			}
		}
		
		//检索消除项
		private function checkRemoveItemHandler(e:GameEvent):void{
			if(mixArr.length == 0){ 
				mouseEnableFlag = true;
				return;
			}
			mouseEnableFlag = false;
			trace("mixArr", mixArr);
			for(var ci:uint = 0; ci < mixArr.length; ci ++){
				var itemElem:Element = mixArr[ci] as Element;
				
				var centerX:uint = itemElem.getCurrentX();
				var centerY:uint = itemElem.getCurrentY();
				
				var tmpCount:uint = 0;
				
				var moveFlag:String = itemElem.getMoveFlag();//放入顺序原则：从左往右，从下往上
				//此元素已交换过，故moveFlag是对方的
				/*if(moveFlag == "right" || moveFlag == "left"){
					//竖
					if(centerY > 0 && centerY < maxCellNum - 1)
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, (centerY + 1), centerX, centerY, centerX, (centerY - 1));
					if(centerY > 1)
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, centerY, centerX, (centerY - 1), centerX, (centerY - 2));
					if(centerY < maxCellNum - 2)
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, (centerY + 2), centerX, (centerY + 1), centerX, centerY);
					//横
					if(moveFlag == "left" && centerX < maxCellNum - 2) 
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, centerY, (centerX + 1), centerY, (centerX + 2), centerY);
					else if(moveFlag == "right" && centerX > 1)	
						tmpCount += pushElementToRemoveElemArr(itemElem, (centerX - 2), centerY, (centerX - 1), centerY, centerX, centerY);
				}
				else if(moveFlag == "up" || moveFlag == "down"){
					//横
					if(centerX > 0 && centerX < maxCellNum - 1)	
						tmpCount += pushElementToRemoveElemArr(itemElem, (centerX - 1), centerY, centerX, centerY, (centerX + 1), centerY);
					if(centerX > 1)	
						tmpCount += pushElementToRemoveElemArr(itemElem, (centerX - 2), centerY, (centerX - 1), centerY, centerX, centerY);
					if(centerX < maxCellNum - 2) 
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, centerY, (centerX + 1), centerY, (centerX + 2), centerY);
					//竖
					if(moveFlag == "down" && centerY > 1)
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, centerY, centerX, (centerY - 1), centerX, (centerY - 2));
					else if(moveFlag == "up" && centerY < maxCellNum - 2)
						tmpCount += pushElementToRemoveElemArr(itemElem, centerX, (centerY + 2), centerX, (centerY + 1), centerX, centerY);
				} */
				
				if(centerX > 1)	
					tmpCount += pushElementToRemoveElemArr((centerX - 2), centerY, (centerX - 1), centerY, centerX, centerY);
				if(centerX > 0 && centerY > 0 && centerY < maxCellNum - 1)
					tmpCount += pushElementToRemoveElemArr((centerX - 1), (centerY + 1), (centerX - 1), centerY, (centerX - 1), (centerY - 1));
				
				if(centerX > 0 && centerX < maxCellNum - 1 && centerY < maxCellNum - 1)
					tmpCount += pushElementToRemoveElemArr((centerX - 1), (centerY + 1), centerX, (centerY + 1), (centerX + 1), (centerY + 1));
				if(centerY < maxCellNum - 2)
					tmpCount += pushElementToRemoveElemArr(centerX, (centerY + 2), centerX, (centerY + 1), centerX, centerY);
				
				if(centerY > 0 && centerY < maxCellNum - 1)
					tmpCount += pushElementToRemoveElemArr(centerX, (centerY + 1), centerX, centerY, centerX, (centerY - 1));
				if(centerX > 0 && centerX < maxCellNum - 1)	
					tmpCount += pushElementToRemoveElemArr((centerX - 1), centerY, centerX, centerY, (centerX + 1), centerY);
				
				if(centerY > 1)
					tmpCount += pushElementToRemoveElemArr(centerX, centerY, centerX, (centerY - 1), centerX, (centerY - 2));
				if(centerX > 0 && centerX < maxCellNum - 1 && centerY > 0)
					tmpCount += pushElementToRemoveElemArr((centerX - 1), (centerY - 1), centerX, (centerY - 1), (centerX + 1), (centerY - 1));
					
				if(centerX < maxCellNum - 1 && centerY > 0 && centerY < maxCellNum - 1)
					tmpCount += pushElementToRemoveElemArr((centerX + 1), (centerY + 1), (centerX + 1), centerY, (centerX + 1), (centerY - 1));
				if(centerX < maxCellNum - 2) 
					tmpCount += pushElementToRemoveElemArr(centerX, centerY, (centerX + 1), centerY, (centerX + 2), centerY);
				
				//itemElem.setReadyFlag(true);
				itemElem.setPlusCount(tmpCount);
			}
			trace("removeElemArr", removeElemArr);
			//全部结束
			if(removeElemArr.length == 0){
				mouseEnableFlag = true;
			
				if(mixArr.length == 2 && (mixArr[0] as Element).getMoveFlag() != (mixArr[1] as Element).getMoveFlag()){
					//两个元素交换，且没有可消除内容，恢复交换
					mixArr[1].dispatchEvent(new GameEvent(GameEvent.ElementMoveBack));
				}
				else{
					//mixArr = new Array();
					trace("return1");
					return;
				}
			}
			
			while(mixArr.length > 0){
				var mE = mixArr.shift();
				var plus:uint = mE.getPlusCount();
				//trace("plus", plus);
				if(plus > 0){
					//计算分值 3个5分，每多一个多5分
					var scoreTmp = (plus - 3) * 5 + 5;
					
					//显示分数标签
					var sT:ScoreTip = new ScoreTip();
					bgMc.addChild(sT);
					setRealPosition(sT, mE.getCurrentX(), mE.getCurrentY());
					sT.score_txt.text = scoreTmp;
					
					toolbarMc.score_txt.text = int(toolbarMc.score_txt.text) + scoreTmp;
				}
			}
			
			this.dispatchEvent(new GameEvent(GameEvent.DoRemove));
		}
		
		//将符合要求的元素推入待消除数组
		private function pushElementToRemoveElemArr(xVal0:uint, yVal0:uint, xVal1:uint, yVal1:uint, xVal2:uint, yVal2:uint):uint{
			var returnInt:uint = 0;
			
			var index0, index1,index2:uint;
			var elm0, elm1,elm2;
			
			elm0 = cellArr[xVal0][yVal0];
			if(elm0 == undefined || elm0 == 0) return returnInt;
			index0 = (elm0 as Element).getIndex();
			
			elm1 = cellArr[xVal1][yVal1];
			if(elm1 == undefined || elm1 == 0) return returnInt;
			index1 = (elm1 as Element).getIndex();
					
			elm2 = cellArr[xVal2][yVal2];
			if(elm2 == undefined || elm2 == 0) return returnInt;
			index2 = (elm2 as Element).getIndex();
			
			if(index1 == index0 && index2 == index0){
				if(removeElemArr.indexOf(elm0) == -1){
					spliceIntoArr(elm0);
					returnInt ++;
					dropArr[xVal0] += 1;
				}
				if(removeElemArr.indexOf(elm1) == -1){
					spliceIntoArr(elm1);
					returnInt ++;
					dropArr[xVal1] += 1;
				}
				if(removeElemArr.indexOf(elm2) == -1){
					spliceIntoArr(elm2);
					returnInt ++;
					dropArr[xVal2] += 1;
				}
			}
			
			return returnInt;
		}
		
		private function spliceIntoArr(e:Element):void{
			//和之前的元素比位置，按照从左往右，从下往上的原则，保证最下和最左的先入列，先出列计算
			if(removeElemArr.length > 0){
				var eNum:uint = e.getCurrentX() * maxCellNum + (maxCellNum - e.getCurrentY());
				
				for(var ei:uint = 0; ei < removeElemArr.length; ei ++){
					var tmpE:Element = removeElemArr[ei];
					var tmpNum:uint = tmpE.getCurrentX() * maxCellNum + (maxCellNum - tmpE.getCurrentY());
					
					if(tmpNum > eNum){
						removeElemArr.splice(ei, 0, e);
						return;
					}
				}
			}
			removeElemArr.push(e);
		}
		
		//执行移除
		private function doRemoveHandler(e:GameEvent):void{
			for(var tti:uint = 0; tti < removeElemArr.length; tti ++){
				var tmpE:Element = removeElemArr[tti];
				trace("cellNum", tmpE.getCurrentX(), tmpE.getCurrentY(), tmpE.getCurrentX() * maxCellNum + (maxCellNum - tmpE.getCurrentY()));
				tmpE.gotoAndPlay("distroy");
				
				var ttX = tmpE.getCurrentX();
				var ttY = tmpE.getCurrentY();
				cellArr[ttX][ttY] = undefined;
				
				//关于路径
				var pp = pathPointArr[ttX][ttY];
				if(pp != undefined && pp.visible == false){
					pp.visible = true;
				}
			}
			
			var monkeyWalkArr:Array = new Array();
			
			while(pathArr.length > 0){
				var ttA:Array = pathArr[0];
				var ttP = pathPointArr[ttA[0]][ttA[1]];
				if(ttP.visible == false && cellArr[ttA[0]][ttA[1]] != undefined) break;// && cellArr[ttA[0]][ttA[1]].currentFrame < distroyFrame
				monkeyWalkArr.push(pathArr.shift());
			}
			
			var mlen:uint = monkeyWalkArr.length;
			if(mlen > 0){
				//旧主角位置多余
				var monkeyOldX:uint = monkeyMc.getCurrentX();
				var monkeyOldY:uint = monkeyMc.getCurrentY();
				var unvisibleE:Element = new Element(monkeyOldX, monkeyOldY, 1);
				elementSp.addChild(unvisibleE);
				unvisibleE.visible = false;
				spliceIntoArr(unvisibleE);
				cellArr[monkeyOldX][monkeyOldX] = unvisibleE;
				dropArr[monkeyMc.getCurrentX()] += 1;
				
				//新主角位置空出
				var newX:uint = monkeyWalkArr[mlen - 1][0];
				var newY:uint = monkeyWalkArr[mlen - 1][1];
				cellArr[newX][newY] = 0;
				dropArr[newX] -= 1;
				
				monkeyMc.dispatchEvent(new GameEvent(GameEvent.MonkeyMove, [monkeyWalkArr]));
			}
			else
				this.dispatchEvent(new GameEvent(GameEvent.ElementsDrop));
		}
		
		//移除后的上部元素掉落
		private function elementsDropHandler(e:GameEvent):void{
			var tmpNumPlus:uint = 0;
			trace("removeElemArr2", removeElemArr);
			trace("dropArr", dropArr);
			while(removeElemArr.length > 0){
				var tmpE:Element = removeElemArr.shift();
				
				var ttX = tmpE.getCurrentX();
				var ttY = tmpE.getCurrentY();
				var ttNum = dropArr[ttX];
				
				if(ttNum == 0){trace("return3");continue;}
				var tmpCell;
				var tmpCount:uint = 0;
				for(var tt:uint = ttY - 1; tt >= 0; tt --){
					if(tt > ttY) break;
					
					
					/*if(tt - ttNum < 0){
						//continue;
						var index:uint = getIndex(ttX, tt, false);//uint(Math.random() * cellKindNum) + 1;
						
						tmpCell = new Element(ttX, tt, index);
						elementSp.addChild(tmpCell);
						setRealPosition(tmpCell, ttX, tt - ttNum);
					}
					else{
						tmpCell = cellArr[ttX][tt - ttNum];
						if(tmpCell == undefined){ trace("return4");continue;}
						
						//如果是monkey，跳过，并且下一个多落一次
						if(tmpCell == 0){
							tmpNumPlus = 1;
							trace("return5");
							continue;
						}
					}*/
					
					tmpCell = cellArr[ttX][tt];
					if(tmpCell == undefined){
						
							trace("return6", tmpCell, ttX, tt); 
							continue;
						
					}
					else{
						cellArr[ttX][tt] = undefined;
						tmpCell.setCurrentY(ttY - tmpCount);
					}
					cellArr[ttX][ttY - tmpCount] = tmpCell;
					
					//cellArr[ttX][tt - ttNum] = undefined;
					//cellArr[ttX][tt + tmpNumPlus] = tmpCell;
					//tmpCell.setCurrentY(tt + tmpNumPlus);
					
					tmpCell.setMoveFlag("down");
					if(mixArr.indexOf(tmpCell) == -1) mixArr.push(tmpCell);
					setRealPosition(tmpCell, ttX, ttY - tmpCount);//setRealPosition(tmpCell, ttX, tt + tmpNumPlus);
					//tmpCell.dispatchEvent(new GameEvent(GameEvent.ElementDrop, [ttY - tt]));
					tmpCount ++;
					//break;
					//mouseEnableFlag = true;
					//if(tmpNumPlus == 1) tmpNumPlus = 0;
				}
				//确保只落一次
				dropArr[ttX] = 0;
				
				for(var tn:uint = ttNum - 1; tn >= 0; tn --){
					if(tn > ttNum)break;
					
					var index:uint = uint(Math.random() * cellKindNum) + 1;//getIndex(ttX, tn, false);//
					
					tmpCell = new Element(ttX, tn, index);
					elementSp.addChild(tmpCell);
					setRealPosition(tmpCell, ttX, tn);
					tmpCell.setMoveFlag("down");
					if(mixArr.indexOf(tmpCell) == -1) mixArr.push(tmpCell);
					//tmpCell.dispatchEvent(new GameEvent(GameEvent.ElementDrop, [tn + 1]));
				}
			}
			
			
			//延时触发
			frameCount = 0;
			this.addEventListener(Event.ENTER_FRAME, delayAnimationHandler);
		}
		
		private function delayAnimationHandler(e:Event):void{
			if(frameCount == 24){
				/*for(var ii = 0; ii < cellArr.length; ii ++){
					for(var ij = 0; ij < cellArr[ii].length; ij ++){
						if(cellArr[ii][ij] == undefined) trace("x");
						else if(cellArr[ii][ij] == 0) trace("monkey");
						else{ 
							trace((cellArr[ii][ij] as Element).getCurrentX(), (cellArr[ii][ij] as Element).getCurrentY(), (cellArr[ii][ij] as Element).getIndex());
							setRealPosition(cellArr[ii][ij] as Element, ii, ij);
						}
					}
				}*/
				
				this.dispatchEvent(new GameEvent(GameEvent.CheckRemove));
				
				this.removeEventListener(Event.ENTER_FRAME, delayAnimationHandler);
			}
			frameCount ++;
		}
	}
	
}
